//
//  TaskingViewController.m
//  oa
//
//  Created by WisdomsLab on 14-3-9.
//  Copyright (c) 2014年 Juchui. All rights reserved.
//

#import "TaskingViewController.h"
#import "DTAttributedTextCell.h"
#import "RoboViewController.h"

NSString * const AttributedTextRecordCellReuseIdentifier = @"AttributedTextRecordCellReuseIdentifier";

@interface TaskingViewController ()<
UITableViewDataSource,
UITableViewDelegate,
RoboViewControllerDelegate
>{
    NSCache *cellCache;
}

@property(strong,nonatomic) NSMutableArray *dataSource;

@end

@implementation TaskingViewController

@synthesize dataSource;
@synthesize recordTableView;
@synthesize publicTableView;
@synthesize privateTableView;
@synthesize opinionText;
@synthesize btnSave;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    cellCache = [[NSCache alloc] init];
    
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
	[self.recordTableView registerClass:[DTAttributedTextCell class] forCellReuseIdentifier:AttributedTextRecordCellReuseIdentifier];
#endif
    
    self.recordTableView.delegate = self;
    self.recordTableView.dataSource = self;
    self.recordTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.publicTableView.delegate = self;
    self.publicTableView.dataSource = self;
    
    self.privateTableView.delegate = self;
    self.privateTableView.dataSource = self;
    
    dataSource = [[NSMutableArray alloc] initWithArray:@[
                                                         @{@"title":@"拟 稿 人",@"content":@"张三"},@{@"title":@"文件标题",@"content":@"李四"},@{@"title":@"审阅内容",@"content":@"王xx 2014-03-09"}
                                                         ]
                  ];
    
    
    @weakify(self);
    self.btnSave.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
        return [RACSignal empty];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.publicTableView reloadData];
    [self.privateTableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -

-(void)showPdfWithName:(NSString *)name{
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"pdf"];
    RoboDocument *document = [[RoboDocument alloc] initWithFilePath:path password:@""];
    RoboViewController *roboViewController = [[RoboViewController alloc] initWithRoboDocument:document];
    roboViewController.delegate = self;
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    [self presentViewController:roboViewController animated:YES completion:^{
        
    }];
}

- (BOOL)canReuseCells
{
	// reuse does not work for variable height
	
	if ([self respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]){
		return NO;
	}
	
	// only reuse cells with fixed height
	return YES;
}

- (void)configureCell:(DTAttributedTextCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
	NSDictionary *snippet = dataSource[indexPath.row];
	NSString *title = [snippet objectForKey:@"title"];
	NSString *content = [snippet objectForKey:@"content"];
    
	if([title isEqualToString:@"审阅内容"]){
        NSMutableString *html = [[NSMutableString alloc] initWithFormat:@"<h3><font color='red'>%@</font></h3>", title];
        for(int i=1;i<10;i++){
            NSString *name = [NSString stringWithFormat:@"<p style='text-indent: 4em;'>张x%zi                 2014-03-0%zi</p>",i,i];
            [html appendString:name];
        }
        [cell setHTMLString:html];
    }else{
        NSString *html = [NSString stringWithFormat:@"<h3><font color='red'>%@</font></h3><p style='text-indent: 4em;'><font color=\"gray\">%@</font></p>", title, content];
        [cell setHTMLString:html];
    }
	
	cell.attributedTextContextView.shouldDrawImages = YES;
}

- (DTAttributedTextCell *)tableView:(UITableView *)tableView preparedCellForIndexPath:(NSIndexPath *)indexPath{
	// workaround for iOS 5 bug
	NSString *key = [NSString stringWithFormat:@"%ld-%ld", (long)indexPath.section, (long)indexPath.row];
	
	DTAttributedTextCell *cell = [cellCache objectForKey:key];
    
	if (!cell)
	{
		if ([self canReuseCells]){
			cell = (DTAttributedTextCell *)[tableView dequeueReusableCellWithIdentifier:AttributedTextRecordCellReuseIdentifier];
		}
        
		if (!cell){
			cell = [[DTAttributedTextCell alloc] initWithReuseIdentifier:AttributedTextRecordCellReuseIdentifier];
		}
		
//		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.hasFixedRowHeight = NO;
		
		// cache it, if there is a cache
		[cellCache setObject:cell forKey:key];
	}
	
	[self configureCell:cell forIndexPath:indexPath];
	
	return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//	if (_useStaticRowHeight)
//	{
//		return tableView.rowHeight;
//	}
	
	DTAttributedTextCell *cell = (DTAttributedTextCell *)[self tableView:tableView preparedCellForIndexPath:indexPath];
    
	return [cell requiredRowHeightInTableView:tableView];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if([tableView isEqual:self.recordTableView]){
        return dataSource.count;
    }
    
    if([tableView isEqual:self.publicTableView]){
        return 1;
    }
    
    if([tableView isEqual:self.privateTableView]){
        return 1;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if([tableView isEqual:self.recordTableView]){
        DTAttributedTextCell *cell = (DTAttributedTextCell *)[self tableView:tableView preparedCellForIndexPath:indexPath];
        return cell;
    }
    
    if([tableView isEqual:self.publicTableView]){
        static NSString *CellIdentifier = @"PublicAttachCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        if(nil == cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        
        cell.textLabel.text = @"developer_license.pdf";
        return cell;
    }
    
    if([tableView isEqual:self.privateTableView]){
        static NSString *CellIdentifier = @"PrivateAttachCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        if(nil == cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        
        cell.textLabel.text = @"multithread.pdf";
        return cell;
    }
    
    return nil;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([tableView isEqual:self.publicTableView]){
        [self showPdfWithName:@"developer_license"];
    }
    
    if([tableView isEqual:self.privateTableView]){
        [self showPdfWithName:@"multithread"];
    }
}

#pragma mark - RoboViewControllerDelegate

- (void)dismissRoboViewController:(RoboViewController *)viewController{
    [viewController dismissViewControllerAnimated:YES completion:^{
    }];
}

@end
